<template>
    <div v-if="proxy.isAuth(['ROOT', 'RULE:SELECT'])">
        <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="form">
            <el-form-item prop="name">
                <el-input v-model="dataForm.name" placeholder="规则名称" maxlength="20" class="input"
                    clearable="clearable" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchHandle()">查询</el-button>
                <el-button type="primary" :disabled="!proxy.isAuth(['ROOT', 'RULE:INSERT'])" @click="addHandle()">
                    新增
                </el-button>
            </el-form-item>
        </el-form>
    </div>
    <el-table :data="data.dataList" :header-cell-style="{ 'background': '#f5f7fa' }" border v-loading="data.loading">
        <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
            <template #default="scope">
                <span>{{ (data.pageIndex - 1) * data.pageSize + scope.$index + 1 }}</span>
            </template>
        </el-table-column>
        <el-table-column prop="name" header-align="left" align="left" min-width="150" label="规则名称" />
        <el-table-column prop="remark" header-align="left" align="left" min-width="350" label="备注信息" />
        <el-table-column prop="count" header-align="center" align="center" min-width="80" label="关联套餐数量" />
        <el-table-column header-align="center" align="center" width="150" label="操作">
            <template #default="scope">
                <el-button type="text" v-if="proxy.isAuth(['ROOT', 'RULE:UPDATE'])" @click="updateHandle(scope.row.id)">
                    修改
                </el-button>
                <el-button type="text" :disabled="scope.row.count > 0" v-if="proxy.isAuth(['ROOT', 'RULE:DELETE'])"
                    @click="deleteHandle(scope.row.id)">
                    删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="data.pageIndex"
        :page-sizes="[10, 20, 50]" :page-size="data.pageSize" :total="data.totalCount"
        layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 创建弹窗控件-添加规则 -->
    <el-dialog :title="!dialog.dataForm.id ? '新增' : '修改'" v-if="proxy.isAuth(['ROOT', 'RULE:INSERT', 'RULE:UPDATE'])"
        :close-on-click-modal="false" v-model="dialog.visible" custom-class="dialog" width="500px">
        <el-form :model="dialog.dataForm" ref="dialogForm" :rules="dialog.dataRule" label-width="80px">
            <el-form-item label="规则名称" prop="name">
                <el-input v-model="dialog.dataForm.name" maxlength="20" clearable />
            </el-form-item>
            <el-form-item label="规则内容" prop="rule">
                <el-input v-model="dialog.dataForm.rule" type="textarea" rows="5" clearable />
            </el-form-item>
            <el-form-item label="备注信息">
                <el-input v-model="dialog.dataForm.remark" type="textarea" rows="3" clearable />
            </el-form-item>
        </el-form>

        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialog.visible = false">取消</el-button>
                <el-button type="primary" @click="dataFormSubmit">确定</el-button>
            </span>
        </template>
    </el-dialog>

</template>

<script lang="ts" setup>
import { reactive, getCurrentInstance, ref, onMounted } from 'vue';
import { Delete } from '@element-plus/icons-vue';
import router from '../../router/index';
const { proxy } = getCurrentInstance() as any;

const dataForm = reactive({
    name: null
});

const dataRule = reactive({
    name: [
        { required: false, pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$', message: '规则名称不正确' }
    ]
});
const data = reactive({
    dataList: [],
    pageIndex: 1,
    pageSize: 10,
    totalCount: 0,
    loading: false
});
const dialog = reactive({
    visible: false,
    dataForm: {
        id: null as number | null,
        name: null as string | null,
        remark: null as string | null,
        rule: null as string | null
    },
    dataRule: {
        name: [
            { required: true, message: '规则名称不能为空' },
            { pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$', message: '规则名称不正确' }
        ],
        rule: [{ required: true, trigger: 'blur', message: '规则内容不能为空' }]
    }
});


//加载分页数据
function loadDataList() {
    data.loading = true;
    let json = {
        page: data.pageIndex,
        length: data.pageSize,
        name: dataForm.name
    };
    proxy.$http('/mis/rule/searchByPage', 'POST', json, true, function (resp: any) {
        let page = resp.page;
        let list = page.list;
        data.dataList = list;
        data.totalCount = page.totalCount;
        data.loading = false;
    });
}

loadDataList();

//查询处理
function searchHandle() {
    proxy.$refs['form'].validate((valid: boolean) => {
        if (valid) {
            proxy.$refs['form'].clearValidate();
            if (dataForm.name == '') {
                dataForm.name = null;
            }
            if (data.pageIndex != 1) {
                data.pageIndex = 1;
            }
            loadDataList();
        } else {
            return false;
        }
    });
}

//分页
function sizeChangeHandle(val: number) {
    data.pageSize = val;
    data.pageIndex = 1;
    loadDataList();
}
function currentChangeHandle(val: number) {
    data.pageIndex = val;
    loadDataList();
}

//添加处理
function addHandle() {
    dialog.dataForm.id = null;
    //因为remark是非必填数据，所以需要我们自己清空
    dialog.dataForm.remark = null;
    dialog.visible = true;
    proxy.$nextTick(() => {
        proxy.$refs['dialogForm'].resetFields();
    });
}

//提交数据(添加与修改)
function dataFormSubmit() {
    proxy.$refs['dialogForm'].validate((valid: boolean) => {
        if (valid) {
            proxy.$http(
                `/mis/rule/${dialog.dataForm.id == null ? 'insert' : 'update'}`,
                'POST',
                dialog.dataForm,
                true,
                function (resp: any) {
                    if (resp.rows == 1) {
                        proxy.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1200,
                            onClose: () => {
                                dialog.visible = false;
                                loadDataList();
                            }
                        });

                    } else {
                        proxy.$message({
                            message: '操作失败',
                            type: 'error',
                            duration: 1200
                        });
                    }
                }
            );
        }
    });
}

//修改处理
function updateHandle(id: number) {
    dialog.dataForm.id = id;
    dialog.visible = true;
    proxy.$nextTick(() => {
        proxy.$refs['dialogForm'].resetFields();
        let json = { id: id };
        proxy.$http('/mis/rule/searchById', 'POST', json, true, function (resp: any) {
            let result = resp.result;
            dialog.dataForm.name = result.name;
            dialog.dataForm.rule = result.rule;
            dialog.dataForm.remark = result.remark;
        });
    });
}

//删除处理
function deleteHandle(id: number) {
    proxy.$confirm(`确定要删除选中的记录？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        let json = { id: id };
        proxy.$http('/mis/rule/deleteById', 'POST', json, true, function (resp: any) {
            if (resp.rows > 0) {
                proxy.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1200,
                    onClose: () => {
                        loadDataList();
                    }
                });
            } else {
                proxy.$message({
                    message: '未能删除记录',
                    type: 'warning',
                    duration: 1200
                });
            }
        });
    });
}

</script>
